import {
  Lunar
} from 'lunar-javascript'
import {
  showInterstitialAd
} from '../../utils/ad'

Page({
  data: {
    hour: '00',
    minute: '00',
    seconds: '00',
    solarDate: '',
    weekDay: '',
    lunarDate: '',
    yi: '',
    ji: '',
    sections: [{
        title: "常用工具",
        items: [{
            title: "育儿补贴",
            icon: "icon-yuerbutie",
            url: "/subpackages/chart/pages/child-subsidy/index"
          },
          {
            title: "退休年龄",
            icon: "icon-yanglao",
            url: "/subpackages/subpkg1/pages/retirement_input/index"
          },
          {
            title: "人民币大写",
            icon: "icon-currency",
            url: "/subpackages/subpkg1/pages/cn_money/index"
          },
          {
            title: "图片水印",
            icon: "icon-watermark",
            url: "/subpackages/subpkg1/pages/watermark/index"
          },
          {
            title: "复利计算",
            icon: "icon-fuli",
            url: "/subpackages/chart/pages/compound-interest/index"
          },
          {
            title: "安妮口算",
            icon: "icon-kousuan",
            url: "wx170e1d55e1adc928"
          }

        ]
      },
      {
        title: "查询/对照",
        items: [{
            title: "身高体重",
            icon: "icon-shengaotizhong",
            url: "/subpackages/subpkg1/pages/height-weight-table/index"
          },
          {
            title: "衣裤鞋尺码",
            icon: "icon-yifuchimaduibi",
            url: "/subpackages/subpkg1/pages/size-comparison/index"
          },
        ]
      },
      {
        title: "测试",
        items: [{
            title: "MBTI性格",
            icon: "icon-mbti",
            url: "/subpackages/subpkg1/pages/mbti/index"
          },
          {
            title: "广告",
            icon: "",
            url: ""
          },
          {
            title: "九型人格",
            icon: "icon-jiuxing",
            url: "/subpackages/chart/pages/enneagram/test/index"
          },
          {
            title: "BMI肥胖",
            icon: "icon-BMI-1",
            url: "/subpackages/subpkg1/pages/bmi/index"
          }
        ]
      },
      {
        title: "推荐小程序",
        items: [{
            title: "广告",
            icon: "",
            url: ""
          },
          {
            title: "广告",
            icon: "",
            url: ""
          },
          {
            title: "广告",
            icon: "",
            url: ""
          }
        ]
      },
      {
        title: "其他工具",
        items: [{
            title: "垂直文字",
            icon: "icon-text_vertical",
            url: "/subpackages/subpkg1/pages/vertical/index"
          },
          {
            title: "二维码生成",
            icon: "icon-qr_code",
            url: "/subpackages/subpkg1/pages/qr_code/index"
          },
          {
            title: "二维码扫描",
            icon: "icon-scan",
            url: "/subpackages/subpkg1/pages/scan_code/index"
          }
        ]
      }
    ]
  },

  onLoad() {
    this.updateDateInfo();
    this.updateTime();
    this.startClock();
    showInterstitialAd()
  },

  startClock() {
    setInterval(() => {
      this.updateTime();
    }, 1000);
  },

  updateTime() {
    const now = new Date();
    this.setData({
      hour: now.getHours().toString().padStart(2, '0'),
      minute: now.getMinutes().toString().padStart(2, '0'),
      seconds: now.getSeconds().toString().padStart(2, '0')
    });
  },

  updateDateInfo() {
    const now = new Date();
    const lunar = Lunar.fromDate(now);
    const weekMap = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];

    const dayYi = lunar.getDayYi().slice(0, 2).join('、');
    const dayJi = lunar.getDayJi().slice(0, 2).join('、');

    this.setData({
      solarDate: `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日`,
      weekDay: weekMap[now.getDay()],
      lunarDate: `农历${lunar.getMonthInChinese()}月${lunar.getDayInChinese()}`,
      yi: dayYi,
      ji: dayJi
    });
  },

  ontapItem(e) {
    const item = e.currentTarget.dataset.item;
    if (item.url) {
      if (this.isAppId(item.url)) {
        this.navigateToMiniProgram(item.url);
      } else {
        this.navigateToPage(item.url);
      }
    } else {
      console.warn('该项目无跳转链接');
    }
  },
  isAppId(url) {
    return /^wx[0-9a-fA-F]{16}$/.test(url); // 微信AppID格式：wx + 16位字母/数字
  },
  navigateToPage(url) {
    wx.navigateTo({
      url,
      fail: (err) => {
        console.error('跳转失败:', err);
        this.showToast('跳转失败');
      }
    });
  },

  showToast(title) {
    wx.showToast({
      title,
      icon: 'none',
      duration: 2000
    });
  },

  goToAlmanacDetail() {
    wx.navigateTo({
      url: '/subpackages/subpkg1/pages/almanac-detail/index'
    });
  },
  // 阻止事件冒泡
  stopPropagation: function (e) {
    e.stopPropagation(); // 阻止事件向上冒泡
  },

  navigateToMiniProgram(appId) {
    // 在触发跳转的地方调用
    wx.navigateToMiniProgram({
      appId: appId, // 必填
      path: 'pages/index/index', // 可选，跳转的页面路径
      extraData: { // 可选，需要传递给目标小程序的数据
        foo: 'bar'
      },
      envVersion: 'release', // 可选，release/trial/develop
      success(res) {
        // 打开成功
      },
      fail(err) {
        console.log('跳转失败', err)
      }
    })
  },

  onShareAppMessage() {}
});